const { Op } = require("sequelize/dist")
const User = require("../model/User")
const md5 = require("md5");
const { json } = require("express");
//添加User
exports.addUser= async function(name,paw,readroot,mobile,type,sex,birthday){
    paw = md5(paw);
    const result = User.create({
        name,
        paw,
        readroot,
        mobile,
        type,
        sex,
        birthday
    });
    return result

}
//获取用户
exports.getUser = async function(mobile,paw){
    paw = md5(paw)
    const user = await User.findOne({
        attributes:{exclude:["paw","createdAt","deletedAt","updatedAt"]},
       where:{
           [Op.and]:[
               {mobile},
               {paw}
           ]
       } 
    })

    return user;

}
//查找指定用户
exports.findAllUser = async function(id=-1,name=-1,sex=-1,type=-1,mobile=-1){
    let where = {};
   if(name !== -1) {
        where.name = name
    }
    if(id !== -1) {
        where.id = id
    }
    if (sex !==-1) {
        where.sex = sex;
    }
    if (type !==-1) {
        where.type = type
    }
    if (mobile !==-1) {
        where.mobile = mobile;
    }

    const result = User.findAll({
        where,
        
    });
}
//更新用户数据
exports.updataUser = async function(obj={},id){
   const result =  await User.update(obj,{
        where:{
           id
        }
    });
  return result;


}
// 
exports.getUserId = async function(id){
    const result = await User.findByPk(id,{
        attributes:{exclude:["paw","createdAt","deletedAt","updatedAt","id"]},
    });
    return result;
}

//密码验证
exports.isPaw = async function(id){
    const result = await User.findByPk(id,{
        attributes:["paw"]
    })

    return result;
}
exports.getAllUser = async function(page = 1,limit = 10){
    console.log("getalluser",page,limit);
    const result = await User.findAndCountAll({
        attributes:{exclude:["paw","createdAt","deletedAt","updatedAt"]},
        offset:(page-1)*limit,
        limit
    })

    return result;
}


exports.deleteUser = async function(id){

    const result = await User.destroy({
        where:{
            id
        }
    });

    return result;
}